using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using DailyReport.Core.Data.Dao;
using DailyReport.Core.Data.Model;
using DailyReport.Core.Util;
using NHibernate;

namespace DailyReport.NHibernateDao.Data.Dao
{
    public class TaskDao : NHibernateBaseDao<Task, int>, ITaskDao
    {
        public TaskDao()
        {
        }

        Task IGenericDao<Task, int>.GetById(int id, bool shouldLock)
        {
            ISession session = ConnectionManager.Instance.CurrentFactory.OpenSession();

            Task res = new Task();
            ITransaction transaction = session.BeginTransaction();
            session.Load(res, id);
            transaction.Commit();
            session.Close();
            return (res);
        }

        List<Task> IGenericDao<Task, int>.GetAll()
        {
            List<Task> res = new List<Task>();
            ISession session = ConnectionManager.Instance.CurrentFactory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            res = (List<Task>)session.GetNamedQuery("allTasksQuery");

            transaction.Commit();
            session.Close();
            return (res);
        }

        void IGenericDao<Task, int>.Save(Task entity)
        {
            ISession session = ConnectionManager.Instance.CurrentFactory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            session.Save(entity);
            transaction.Commit();
            session.Close();
        }

        void IGenericDao<Task, int>.SaveOrUpdate(Task entity)
        {
            ISession session = ConnectionManager.Instance.CurrentFactory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            session.SaveOrUpdate(entity);
            transaction.Commit();
            session.Close();
        }

        void IGenericDao<Task, int>.Delete(Task entity)
        {
            ISession session = ConnectionManager.Instance.CurrentFactory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            session.Delete(entity);
            transaction.Commit();
            session.Close();
        }
        public IList getInstancesList(string sortExpression, int maxRows, int startRowIndex)
        {
            ISession session = ConnectionManager.Instance.CurrentFactory.OpenSession();
            String queryStr = "from Task";
            if (sortExpression != null && sortExpression.Length > 0)
                queryStr = queryStr + " order by " + sortExpression;

            IQuery querySorteable = session.CreateQuery(queryStr)
                                           .SetFirstResult(startRowIndex)
                                           .SetMaxResults(maxRows);
            IList res = (IList)querySorteable.List();
            session.Close();
            return (res);
        }

        public int getInstancesCount()
        {
            int res;
            ISession session = ConnectionManager.Instance.CurrentFactory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            res = (int)session.GetNamedQuery("tasksCountQuery")
                             .SetMaxResults(1)
                             .UniqueResult();

            transaction.Commit();
            session.Close();
            return (res);
        }
    }
}
